#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    void sortColors(vector<int>& nums) {
        int n = nums.size();
        int i = 0, left = -1, right = n;
        while (i < right)
        {
            if (nums[i] == 1) ++i;
            else if (nums[i] == 2) swap(nums[--right], nums[i]);
            else if (nums[i] == 0) swap(nums[++left], nums[i++]);
        }
    }
};